Repository navigation
fix(logger): child Logger without a level follows its parent level again - #8842
leandrodamascena merged 2 commits into
Conversation
Since aws-powertools#5950 a child Logger always called setLevel(), so without an explicit level it resolved to INFO (or the env var) and stopped inheriting the parent's level. DEBUG logs from child modules were lost when the parent used DEBUG, setLevel() or sampling, and INFO logs leaked when the parent used WARNING. A child now only sets a level when one is passed, and otherwise stays NOTSET like before 3.6.0. The same applies to refresh_sample_rate_calculation(), which used to pin a child to INFO on warm invocations. log_level returns the effective level, so a child reports, and copy_config_to_registered_loggers copies, the level the child actually logs at. Explicit child levels (aws-powertools#5950) and the ALC > explicit level > env var priority are unchanged.
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks @AtulJoshi1206! Only setting a level when one is passed, and keeping initial_log_level at NOTSET so the sampling refresh doesn't pin the child again, is exactly the shape of the fix. On log_level, I agree with the effective level: it keeps child.log_level at 20 for every default child today and copy_config_to_registered_loggers keeps copying a real level. The warm-invocation test is the one I cared most about, good to see it in. Goes out in 3.42.0, no need to rebase, you'll be in the release notes. @saimcyber thanks for the write-up on the root cause, it made this one quick to confirm.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8842 +/- ##
========================================
Coverage 97.15% 97.15%
========================================
Files 327 327
Lines 16780 16781 +1
Branches 1530 1531 +1
========================================
+ Hits 16302 16303 +1
Misses 335 335
Partials 143 143 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
17eca32
into
aws-powertools:develop



Issue number: closes #8778
Summary
Changes
Since #5950 a child Logger always calls
setLevel(). Without alevelit resolves to INFO (orPOWERTOOLS_LOG_LEVEL) and stops following its parent. This PR keeps #5950's behaviour for children that pass aleveland restores inheritance for children that don't:_init_logger: a child only sets its level when one is passed. Otherwise it staysNOTSET, like before 3.6.0.initial_log_level:NOTSETfor a child without a level. Before this,refresh_sample_rate_calculation()(whichinject_lambda_contextcalls on warm invocations) pinned the child back to INFO.log_level: returns the effective level, so a child reports the level it actually logs at. That also makescopy_config_to_registered_loggers(source_logger=child)copy that level. Without this, the first change alone would makechild.log_leveldrop from 20 to 0. This is the open question from the issue thread. I went with the effective level because returning 0 would change whatlog_levelreports for every default child today, but I'm happy to flip it if you'd rather match 3.5.0 exactly.leveldocstring.The priority doesn't change: ALC, then an explicit level (constructor or
setLevel), thenPOWERTOOLS_LOG_LEVEL. A child with an explicit level still goes through the same resolution, including the ALC warning.User experience
Before (3.6.0 to 3.41.0): with
Logger(level="DEBUG")inapp.pyandLogger(child=True)inshared.py, the child's DEBUG logs were dropped. The same happened withsetLevel("DEBUG")orsampling_rate=1on the parent. With a WARNING parent, the child still emitted INFO.After: the child follows its parent again, as in 3.5.0, and a child with its own
levelstill uses it.Visible differences from the current release:
POWERTOOLS_LOG_LEVEL=DEBUGand an explicitLogger(level="INFO")parent, the child now follows the parent (INFO), as it did in 3.5.0.child.log_levelreturns the inherited level. It's 20 with a default parent, so that case is unchanged.child.levelisNOTSETagain.Verification
New tests: I added 9 functional tests, using the public API only. 7 fail on current
developand pass with the fix. The 2 ALC tests pass on both and guard that ALC still wins for children. No existing test was changed. They cover the cases @saimcyber listed: a parentsetLevel()after the child is created, sampling on a warm invocation, and a child with its own level.Logger suites: on current
develop(Python 3.14),pytest tests/unit/logger tests/functional/loggergives 213 passed.Lint and types:
ruff format --check,ruff checkandmypyare clean.Full suite: on Python 3.12,
pytest tests/unit tests/functional -m "not perf"passed apart fromtest_datadog_write_to_log_with_env_variable. That test also fails in a full run ondevelopand passes on its own, so it's order-dependent and unrelated.Cross-version check: I ran the same two-module app against 3.5.0, 3.6.0, 3.38.0 and the fix, covering:
setLevel, and sampling with the decorator;POWERTOOLS_LOG_LEVEL;AWS_LAMBDA_LOG_LEVELset to WARN, INFO and DEBUG.The branch matches 3.5.0 everywhere, except that fix(logger): child logger must respect log level #5950's explicit child level still works. There's no AWS service integration here, so I didn't deploy it; the ALC paths are covered by setting
AWS_LAMBDA_LOG_LEVEL.AI assistance: an AI coding assistant helped prepare this change; it was reviewed and the tests were run locally.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.